#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
 
 

typedef struct LNode
{
    int data;
    struct LNode* next;
}LNode, * LinkList;
LinkList L;
 
 

void CreatList_head(LinkList& L, int num1)
{
    cout << "请依次换行输入" << num1 << "个数据！" << endl;
    L = (LinkList)malloc(sizeof(LNode));
    L->next = NULL;         
    LNode* s;              
    for (int i = 0; i < num1; i++)
    {
        s = (LNode*)malloc(sizeof(LNode));
        cin >> s->data;
        s->next = L->next;  
        L->next = s;		
    }
    cout << "创建成功。" << endl;
}
 
 

void CreatList_tail(LinkList& L, int num1)
{
    cout << "请依次换行输入" << num1 << "个数据" << endl;
    L = (LinkList)malloc(sizeof(LNode));
    L->next = NULL;
    LNode* s;
    LNode* r = L;
    for (int i = 0; i < num1; i++)
    {
        s = (LNode*)malloc(sizeof(LNode));
        cin >> s->data;
        r->next = s;
        r = s;
    }
    r->next = NULL;
    cout << "创建成功。" << endl;

 
 
void showfunction()  
{
    cout << "单链表功能展示" << endl;
    cout << "1.前插法初始化单链表" << endl;     
    cout << "2.尾插法初始化单链表" << endl;     
    cout << "3.输出单链表" << endl;             
    cout << "4.输出单链表长度" << endl;         
    cout << "5.输出单链表第a个元素" << endl;    
    cout << "6.输出元素b的位置" << endl;        
    cout << "7.指定位置插入单个元素" << endl;   
    cout << "8.指定位置删除单个元素" << endl;  
    cout << "9.判断单链表是否为空" << endl;    
    cout << "0.释放单链表" << endl;             
}
 
void print(LinkList& L)  
{
    LNode* node = L->next;
    cout << "表中元素为：";
    if (node == NULL)
    {
        cout << " 该表为空。" << endl;
    }
    else
    {
        while (node != NULL)
        {
            cout << " " << node->data << " ";
            node = node->next;
        }
        cout << endl;
    }
}
 
void ListLength(LinkList& L)   
    int i = 0;
    LNode* p = L;
    while (p->next != NULL) 
    {
        i++;
        p = p->next;
    }
    cout << "单链表长度为"<<i << endl;
 
}
 
int Getbyzhi(LinkList& L, int elem)  
    LNode* temp = L;
 
    int i = 0;
    while (temp->next)
    {
        i = i + 1;
        temp = temp->next;
        if (elem == temp->data)
        {
            cout << "该值在链表中的第" << i << "位" << endl;
            return 0;
        }
    }
    cout << "查找失败！！！" << endl;
    return 0;
}
 
void GetElem(LinkList& L, int place)  
{
    LNode* node = L;
    int j = 0;
    while (node && j < place)
    {
        node = node->next;
        j = j + 1;
    }
    if (!node || j > place)
    {
        cout << "输入的值有问题！！！" << endl;
    }
    else
    {
        cout << "该位置上的值为" << node->data << endl;
    }
}
 
void Insert(LinkList& L, int place1, int elem)    
{
    LNode* p = L;
    int  j = 0;
    while (p && (j < place1 - 1))
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
    {
        cout << "插入失败！！！" << endl;
    }
    else
    {
        LNode* s = (LNode*)malloc(sizeof(LNode));
        s->data = elem;
        s->next = p->next;
        p->next = s;
        cout << "插入成功。" << endl;
    }
}
 
void DeleteElem(LinkList& L, int place3)  
{
    LNode* p = L;
    int j = 0;
    while (p && j < place3 - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
    {
        cout << "删除失败！！！" << endl;
    }
    else
    {
        LNode* newone = p->next;
        p->next = newone->next;
        free(newone);
        cout << "删除成功！！！" << endl;
    }
}
 
void DestroyList(LinkList& L)   
 
    if (NULL ==L->next)
    {
        delete L;
    }
    else
    {
        DestroyList(L->next);
        delete L;
    }
}
 
void ListEmpty(LinkList& L)  
    {
    if (L->next == NULL) 
    {
        cout << "单链表为空" << endl;
    }
    else  
    {
        cout << "单链表不为空" << endl;
    }
}
 
int main() 
{
    while (true) 
    {
        showfunction();  
        int numx;
        cout << "选择要使用的功能" << endl;
        cin >> numx;
        int num1;  
        int place; 
        int place1;  
        int elem1; 
        int elem2;  
        int place3;  
        if (numx > 10)
        {
            cout << "无该功能，请重新选择" << endl;
        }
        else
        {
            switch (numx)
            {
            case 1:
            {
                cout << "输入要插入的数据数" << endl;
                cin >> num1;
                CreatList_head(L, num1);
                system("pause");
                system("cls");
                break;
            }
            case 2: 
            {
                cout << "输入要插入的数据数" << endl;
                cin >> num1;
                CreatList_tail(L, num1);
                system("pause");
                system("cls");
                break;
            }
            case 3:
            {
                print(L);
                system("pause");
                system("cls");
                break;
            }
            case 4: 
            {
                ListLength(L);
                system("pause");
                system("cls");
                break;
            }
            case 5: 
            {
                cout << "要输入元素的位置为" << endl;
                cin >> place;
                GetElem( L, place);
                system("pause");
                system("cls");
                break;
            }
            case 6: 
            {
                cout << "输入要查找的元素" << endl;
                cin >> elem1;
                Getbyzhi(L, elem1);
                system("pause");
                system("cls");
                break;
            }
            case 7:
            {
                cout << "输入位置为" << endl;
                cin >> place1;
                cout << "插入元素为" << endl;
                cin >> elem2;
                Insert(L, place1, elem2);
                system("pause");
                system("cls");
                break;
            }
            case 8: 
            {
                cout << "输入要删除元素的位置" << endl;
                cin >> place3;
                DeleteElem(L, place3);
                system("pause");
                system("cls");
                break;
            }
            case 9:
            {
                ListEmpty(L);
            }
            case 0: 
            {
                DestroyList(L);
            }
            }
        }
    }
}
